<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="uploadParam.supplierChannelId" placeholder="供应商" clearable filterable style="width: 140px;" class="filter-item">
        <el-option v-for="item in supplierChannelOptions" :key="item.key" :label="item.display_name" :value="item.key" />
      </el-select>
      <el-upload
        v-loading="uploadLoading"
        class="avatar-uploader filter-item"
        action="#"
        :show-file-list="false"
        :before-upload="beforeInventoryUpload"
        :http-request="inventoryUpload"
        style="margin-left: 10px;"
      >
        <el-button type="primary" icon="el-icon-upload">
          上传
        </el-button>
      </el-upload>
      <router-link :to="'/manager/channel/supplier'">
        <el-button class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-circle-plus-outline">
          添加供应商
        </el-button>
      </router-link>
    </div>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import { fetchEnum } from '@/api/system-api'
import { uploadInventory } from '@/api/manager/goods-api'
import { toOptionsList } from '@/utils/index'

export default {
  name: 'ManagerInventoryUpload',
  filters: {
  },
  data() {
    return {
      supplierChannelOptions: [],
      uploadParam: {
        supplierChannelId: null
      },
      uploadLoading: false
    }
  },
  async created() {
    await this.initLoad()
  },
  methods: {
    async initLoad() {
      const enumCodes = ['SUPPLIER_CHANNEL']
      const { data } = await fetchEnum(enumCodes)
      for (const e of data) {
        if (e.code === 'SUPPLIER_CHANNEL') {
          if (e.options) {
            this.supplierChannelOptions = toOptionsList(e.options, 'id', 'shortName')
          }
        }
      }
    },

    beforeInventoryUpload(file) {
      var pass = false
      if (this.uploadParam && this.uploadParam.supplierChannelId) {
        pass = true
      } else {
        Message({
          message: '上传库存之前请选择供应商',
          type: 'error',
          duration: 5 * 1000
        })
      }
      return pass
    },
    inventoryUpload(param, type) {
      this.uploadLoading = true
      var formData = new FormData()
      formData.append('file', param.file)
      formData.append('supplierChannelId', this.uploadParam.supplierChannelId)
      uploadInventory(formData).then(response => {
        if (response && response.data && response.data.inventoryCount) {
          Message({
            message: '成功上传库存信息' + response.data.inventoryCount + '条',
            type: 'success',
            duration: 5 * 1000
          })
        } else {
          Message({
            message: response.msg,
            type: 'error',
            duration: 5 * 1000
          })
        }
        this.uploadLoading = false
      }).catch(error => {
        if (error && error.msg) {
          Message({
            message: error.msg,
            type: 'error',
            duration: 5 * 1000
          })
        } else {
          Message({
            message: '文件上传失败',
            type: 'error',
            duration: 5 * 1000
          })
        }
        this.uploadLoading = false
      })
    }

  }
}

</script>
